import { createRouter, createWebHashHistory } from "vue-router";
// import Home from "../views/home/index.vue";

const routes = [
  //重定向
  {
    path: "/",
    redirect: "/home",
  },
  // 首页二级路由
  {
    path: "/home",
    component: () => import("../views/home/Home.vue"),
    children: [
      //重定向
      {
        path: "/home",
        redirect: "/home/index",
      },
      // 首页
      {
        path: "/home/index",
        component: () => import("../views/home/Index.vue"),
      },
      // 课程清单
      {
        path: "/home/couresList",
        component: () => import("../views/home/CouresList.vue"),
        // //路由元信息
        // meta: {
        //   requireLogin: true,
        // },
      },
      // 成就
      {
        path: "/home/achievements",
        component: () => import("../views/home/Achievements.vue"),
      },
      // 我的
      {
        path: "/home/mine",
        component: () => import("../views/home/Mine.vue"),
      },
    ],
  },
  // 一级路由 详情页
  {
    path: "/details/:id",
    component: () => import("../views/details/Details.vue"),
  },
  //一级路由哈佛
  {
    path: "/harvard",
    component: () => import("../views/harvard/Harvard.vue"),
  },
  //一级路由成就系统
  {
    path: "/achievementsList",
    component: () => import("../components/home/AchievementsList.vue"),
    // // 路由元信息
    // meta: {
    //   //说明此页面需要登录
    //   requireLogin: true,
    // },
  },
  // 一级路由登录
  {
    path: "/login",
    component: () => import("../views/login/Login.vue"),
  },
  //一级路由注册
  {
    path: "/register",
    component: () => import("../views/register/Register.vue"),
  },
  //一级路由完善信息
  {
    path: "/logincomplete",
    component: () => import("../views/login_complete/Complete.vue"),
  },
  //一级路由设置
  {
    path: "/setup",
    component: () => import("../views/setup/Setup.vue"),
  },
  //一级路由设备
  {
    path: "/equipment",
    component: () => import("../views/equipment/Equipment.vue"),
  },
  // 一级路由推广
  {
    path: "/promote",
    component: () => import("../views/promote/Promote.vue"),
  },
  // 一级路由商场
  {
    path: "/mall",
    component: () => import("../views/mall/Mall.vue"),
  },
  // 一级路由404
  {
    path: "/:pathMatch(.*)*",
    component: () => import("../views/errors/Errors.vue"),
  },
];

//定义路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

//登陆页面基础登入校验功能
// router.beforeEach((to, from, next) => {
//     const isLogin = localStorage.isLogin;
//     if (isLogin || to.name === "Login") {
//         next();
//     } else {
//         next({ name: "Login" })
//     }
//     next();
// })

//全局前置守卫
// router.beforeEach((to, from, next) => {
//   //需要登录
//   if (to.meta.requireLogin) {
//     if (localStorage.getItem("token")) {
//       next();
//     } else {
//       next("/login");
//     }
//   } else {
//     next();
//   }
// });

// 用于解决router重复点击的报错
const originalPush = createRouter.prototype.push;
createRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

//暴露路由实例
export default router;
